import pytest
import requests
import os
from config.config import Config
from common.consts import DATA_PATH, DINGSTOCK
from common.datas import get_data_only, read_json_no_ids

c = Config()

# @pytest.fixture()
# def admin_token():
#     url = '{}{}'.format(DINGSTOCK, '/api/App/User/LoginExt')
#     payload = {
#         'app_key':c.get_admin('app_key'),
#         'username':c.get_admin('username'),
#         'password':c.get_admin('password')}
#     r = requests.get(url=url,params=payload)
#     return r.json()['data']['token']


# data = get_data_only(os.path.join(DATA_PATH, 'register.yaml'))
#
#
# @pytest.fixture(params=data)
# def register(request, admin_token):
#     datas = request.param
#     url = datas.get('url')
#     payload = datas.get('payload')
#     r = requests.get(url=url, params=payload)
#     yield r.json()['data']['uuid']
#     param = {'app_key': '32D75D3E6B5B16C49E2FDA53C40A87F7',
#              'other_uuid': r.json()['data']['uuid'],
#              'admin_uuid': '5FFBF0ED337AF085531BCA0D1CADC603',
#              'admin_token': admin_token}
#     requests.get(
#         'http://hn216.api.yesapi.cn/api/App/User/RemoveUserForAdmin', params=param)


#
#
# datas = get_data_only(os.path.join(DATA_PATH,'reset_password.yaml'))
#
#
# @pytest.fixture(params=datas)
# def reset_password(request,admin_token):
#     datas = request.param
#     url = '{}{}'.format(DINGSTOCK, datas.get('url'))
#     payload = datas.get('payload')
#     payload['admin_token']=admin_token
#     requests.get(url=url,params=payload)
#     return payload['new_password']


# conftest.py

# 定义一个全局变量，用于存储内容
global_data = {}


@pytest.fixture
def set_global_data():
    """
    设置全局变量，用于关联参数
    :return:
    """

    def _set_global_data(key, value):
        global global_data
        global_data[key] = value

    return _set_global_data


@pytest.fixture
def get_global_data():
    """
    从全局变量global_data中取值
    :return:
    """

    def _get_global_data(key):
        global global_data
        return global_data.get(key)

    return _get_global_data


file = '/Users/ra1chi/PycharmProjects/dingstock_api_auto/datas/test_08_devine_comment.json'
data = read_json_no_ids(file_path=file)


@pytest.fixture(params=data)
def login_dingstock(request):
    """登陆盯潮app，返回session token"""
    datas = request.param
    url = f'{DINGSTOCK}/user/u/login'
    header = {
        "version": "2.13.2",
        "app": "iOS2.13.2(2021110203)",
        "Content-Type": "application/json"
    }
    payload = {
        "app": "iOS2.13.2(2021110203)",
        "deviceId": datas['deviceId'],
        "deviceToken": datas['deviceToken'],
        "authData": {
            "sms": {
                "id": datas['mobileId'],
                "code": "729514",
                "zone": "+86",
                "mobile": datas['mobile']
            }
        }
    }
    r = requests.post(url=url, json=payload, headers=header)
    yield r.json()['res']['sessionToken']
